/*
 *所有关于daojiaogd_gz类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*daojiaogd_gzService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using DGZImp.Core.BaseProvider;
using DGZImp.Core.Extensions.AutofacManager;
using ATkg.BizAppProject.DomainModels;
using System.Linq;
using DGZImp.Core.Utilities;
using System.Linq.Expressions;
using DGZImp.Core.Extensions;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using ATkg.BizAppProject.IRepositories;
using DGZImp.Core.EFDbContext;
using DGZImp.Core.DbSqlSugar;
using DGZImp.Core.Exceptions;
using Newtonsoft.Json.Linq;
using System.Globalization;

namespace ATkg.BizAppProject.Services
{
    public partial class daojiaogd_gzService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Idaojiaogd_gzRepository _repository;//访问数据库
        public ISqlSugarClient Db = AutofacContainerModule.GetService<ServiceDbContext>()?.SqlSugarClient;
        public AmesSyncService ames = AutofacContainerModule.GetService<AmesSyncService>();

        [ActivatorUtilitiesConstructor]
        public daojiaogd_gzService(
            Idaojiaogd_gzRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        [SugarTable("daojiaogd_gz")] // 映射表名
        public class Daojiaogd_gz
        {
            [SugarColumn(IsPrimaryKey = true)] // 主键标识（若存在）
            public string schedule_id { get; set; }
            public string name { get; set; }
            public string raw_lot_id { get; set; }
            public string little_lot { get; set; }
            public string plating { get; set; }
            public string lot_ids { get; set; }
            public string prodcut_spec { get; set; }
            public decimal per_qty { get; set; }
            public decimal product_qty { get; set; }
            public string product_id { get; set; }
        }


        public async Task<object> insert_db_lot(JArray data_cf)
        {
            
            for (int i = 0; i < data_cf.Count; i++)
            {
                Console.WriteLine(data_cf[i]);
                var final_data_info = new List<daojiaogd_gz>();

                Console.WriteLine(data_cf[i]["schedule_id"]);
                Console.WriteLine(data_cf[i]["schedule_id"].GetType());
                final_data_info.Add(new daojiaogd_gz
                {
                    schedule_id = (string)data_cf[i]["schedule_id"],
                    name = (string)data_cf[i]["name"],  // 更新字段1
                    raw_lot_id = (string)data_cf[i]["raw_lot_id"],           // 更新字段2
                    little_lot = (string)data_cf[i]["little_lot"],  // 更新字段1
                    per_qty = (decimal)data_cf[i]["per_qty"]
                });
                var find_data = Db.Queryable<daojiaogd_gz>()
                    .Where(mp => mp.schedule_id == (string)data_cf[i]["name"])
                    .ToList();
                var re = find_data.Count;

                if (re > 0)
                {
                    int rowsAffected = Db.Updateable<Daojiaogd_gz>()
                    .SetColumns(it => new Daojiaogd_gz()
                    {

                        schedule_id = (string)data_cf[i]["schedule_id"],
                        name = (string)data_cf[i]["name"],  // 更新字段1
                        raw_lot_id = (string)data_cf[i]["raw_lot_id"],           // 更新字段2
                        little_lot = (string)data_cf[i]["little_lot"],  // 更新字段1
                        per_qty = (decimal)data_cf[i]["per_qty"]


                    })
                    .Where(it => it.name == (string)data_cf[i]["name"]) // 精确条件
                    .ExecuteCommand();
                }
                else
                {
                    await Db.DoInsertAsync<daojiaogd_gz>(final_data_info);
                }
            }

            return data_cf;
        }

        public async Task<object> search_db_lot(string mes_data)
        {
            Console.WriteLine("进来了！！");
            // 查询数据库中当天最大的code
            // 使用 SqlSugar 的 Queryable 语法
            var result = Db.Queryable<daojiaogd_gz>()  //  
                .Where(m => m.name == mes_data)
                .Select(m => new
                {
                    m.schedule_id,
                    m.little_lot,
                    m.name,
                    m.raw_lot_id,
                    m.per_qty,
                })
                .ToList();                               // 执行查询并返回列表
            return result;
        }
    }
}
